package com.itlong.cloud.iot.service.base.impl;

import com.itlong.cloud.POJO.VO.common.IotInfoVO;
import com.itlong.cloud.POJO.VO.common.ProjectIotInfo;
import com.itlong.cloud.POJO.VO.property.CloudControllerVO;
import com.itlong.cloud.constants.RedisConstant;
import com.itlong.cloud.iot.service.server.AliIotServerService;
import com.itlong.cloud.iot.service.server.HuawServerService;
import com.itlong.cloud.iot.service.base.IotBaseService;
import com.itlong.cloud.iot.service.server.IotInteriorService;
import com.itlong.cloud.service.RedisService;
import com.itlong.cloud.utils.words.StringHandlerUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <desc>
 *      IOT公共业务处理接口
 * </desc>
 *
 * @createDate 2020/01/02
 */
@Service
public class IotBaseServiceImpl implements IotBaseService{
    private static final Logger LOG = LoggerFactory.getLogger(IotBaseServiceImpl.class);
    @Autowired
    private AliIotServerService aliIotService;
    @Autowired
    private HuawServerService huawIotService;
    @Autowired
    IotInteriorService iotInteriorService;
    @Autowired
    RedisService<ProjectIotInfo> redisService;

    /**
     * <desc>
     *      注册设备
     * </desc>
     *
     * @param productKey
     * @param deviceUnique,等同设备名称，产品内唯一，由用户自定义，自身业务只有设备唯一码唯一
     * @return
     * @throws Exception
     */
    @Override
    public boolean registDevice(String productKey, String deviceUnique, String deviceType) throws Exception {
        String[] deviceUniques = StringHandlerUtil.splitString(deviceUnique);
        String tempDeviceUnique = deviceUniques[0];
        ProjectIotInfo projectIotInfo = this.checkIotType(tempDeviceUnique);
        List<IotInfoVO> iotInfos = projectIotInfo.getIotInfos();
        IotInfoVO iotInfoVO = iotInfos.stream().filter(info->
                info.getDeviceType().equals(Integer.valueOf
                        (StringHandlerUtil.getDeviceTypeByDeviceUnique(tempDeviceUnique)))).findFirst().get();
        iotInfoVO.setDeviceUnique(deviceUnique);
        if(projectIotInfo.getIotFlag() == 1 || (projectIotInfo.getIotFlag() != 1 && StringHandlerUtil.getDeviceTypeByDeviceUnique(tempDeviceUnique) != 1
            && StringHandlerUtil.getDeviceTypeByDeviceUnique(tempDeviceUnique) != 5 && StringHandlerUtil.getDeviceTypeByDeviceUnique(tempDeviceUnique) != 2) ||
                "24".equals(deviceType)) {
            //24 为一体式门禁 一体式门不支持华为iot 特殊处理
            if(projectIotInfo.getIotFlag() != 1 && "24".equals(deviceType)) {
                iotInfoVO.setIotProductKey(productKey);
            }
            return aliIotService.registDevice(iotInfoVO);
        }else{
            return huawIotService.registDevice(iotInfoVO);
        }
    }

    /**
     * <desc>
     *     批量获取同一个产品型号下的设备工作状态
     * </desc>
     *
     * @param productKey
     * @param deviceUniques 设备序列号 多个,号分隔
     * @return
     * @author Jiaqi.X
     * @createDate  2018/03/17
     */
    @Override
    public String batchGetDeviceState(String productKey, String deviceUniques) {
        return aliIotService.batchGetDeviceState(productKey,deviceUniques);
    }

    /**
     * <desc>
     *     批量获取同一个产品型号下的设备工作状态并保存到缓存中
     * </desc>
     *
     * @param productKey
     * @param deviceUniques 设备序列号 多个,号分隔
     * @param type 设备型号:1云对讲,2云电梯,3云门禁,4群控器,5联动器,6平板,7一卡通
     * @return
     * @author Qiang.S
     * @createDate  2019/07/04
     */
    @Override
    public void batchGetDeviceStateForOnline(String productKey, List<CloudControllerVO> deviceUniques, String type) {
        aliIotService.batchGetDeviceStateForOnline(productKey,deviceUniques,type);
    }

    /**
     * <desc>
     *      发布消息
     * </desc>
     *
     * @param productKey  产品key
     * @param topic  设备topic地址
     * @param msg 消息内容
     * @author Jiaqi.X
     * @createDate 2018/04/11
     */
    @Override
    public void pubDeviceMessage(String productKey, String topic, String msg,String huaweiMsg,String serviceId,String commandName,String iotFlag) {
        String deviceUnique = topic.substring(1,14);
        ProjectIotInfo projectIotInfo = this.checkIotType(deviceUnique);
        List<IotInfoVO> iotInfos = projectIotInfo.getIotInfos();
        IotInfoVO iotInfoVO = iotInfos.stream().filter(info ->
                info.getDeviceType().equals(Integer.valueOf
                        (StringHandlerUtil.getDeviceTypeByDeviceUnique(deviceUnique)))).findFirst().get();
        iotInfoVO.setDeviceUnique(deviceUnique);
        if(StringUtils.isNotBlank(iotFlag)){
            projectIotInfo.setIotFlag(Integer.valueOf(iotFlag));
        }
        if(projectIotInfo.getIotFlag() == 1 || (projectIotInfo.getIotFlag() != 1 && StringHandlerUtil.getDeviceTypeByDeviceUnique(deviceUnique) != 1
                && StringHandlerUtil.getDeviceTypeByDeviceUnique(deviceUnique) != 5 && StringHandlerUtil.getDeviceTypeByDeviceUnique(deviceUnique) != 2)) {
            aliIotService.pubDeviceMessage(productKey, topic, msg);
        }else{
            if(StringUtils.isNotBlank(projectIotInfo.getHuaweiIotId())) {
                LOG.info("【华为云消息发布，deviceUnique】"+deviceUnique);
                huawIotService.pubDeviceMessage(serviceId, commandName, projectIotInfo.getHuaweiIotId(), huaweiMsg);
            }
        }
    }

    /**
     * <desc>
     *      获取当前项目iot类型
     * </desc>
     *
     * @param deviceUnique
     * @return
     * @author Jiaqi.X
     * @createDate 2020/01/08
     */
    private ProjectIotInfo checkIotType(String deviceUnique){
        String projectId = StringHandlerUtil.getProjectIdByDeviceUnique(deviceUnique);
//        ProjectIotInfo projectIotInfo = redisService.getObject(RedisConstant.PROJECT_IOT_TYPE_REDIS_KEY+ projectId,ProjectIotInfo.class);
//        if(projectIotInfo == null){
        ProjectIotInfo projectIotInfo = iotInteriorService.getIotInfoByDeviceUnique(deviceUnique);
        redisService.set(RedisConstant.PROJECT_IOT_TYPE_REDIS_KEY+projectId,RedisConstant.PROJECT_IOT_TYPE_REDIS_CACHE_TIME,projectIotInfo);
//        }
        return projectIotInfo;
    }
}